﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TvdbLib;
using System.IO;
using System.Net;

namespace TVDBDownloader
{
    class Downloader
    {
        TvdbDownloader tvdbDownloader;
        FanArtTVDownloader fanarttvDownloader;
        TvdbLib.Data.TvdbSearchResult searchResult;
        public enum BannerType {Banner, Poster, FanArt, Logo, ClearArt }

        public Downloader(string serieName)
        {
            tvdbDownloader = new TvdbDownloader("F169626F0457ECDC");
            fanarttvDownloader = new FanArtTVDownloader();
            searchResult = tvdbDownloader.DownloadSearchResults(serieName, TvdbLib.Data.TvdbLanguage.DefaultLanguage).Take(1).FirstOrDefault();
        }


        public void DownloadBanner(DirectoryInfo dirSerie, BannerType type)
        {

            if (Directory.GetFiles(dirSerie.FullName, type.ToString() + ".*").Length == 0)
            {
                if (searchResult != null)
                {
                    TvdbLib.Data.TvdbBanner banner = null;
                    if (type == BannerType.Banner)
                        banner = tvdbDownloader.DownloadBanners(searchResult.Id)
                                            .Where(c => c is TvdbLib.Data.Banner.TvdbSeriesBanner)
                                            .Take(1).FirstOrDefault();
                    else if (type == BannerType.Poster)
                        banner = tvdbDownloader.DownloadBanners(searchResult.Id)
                                            .Where(c => c is TvdbLib.Data.Banner.TvdbPosterBanner)
                                            .Take(1).FirstOrDefault();
                    else if (type == BannerType.FanArt)
                        banner = tvdbDownloader.DownloadBanners(searchResult.Id)
                                            .Where(c => c is TvdbLib.Data.Banner.TvdbFanartBanner)
                                            .Take(1).FirstOrDefault();
                    else if (type == BannerType.Logo)
                        banner = fanarttvDownloader.DownloadBanner(searchResult.Id, BannerType.Logo);
                    else if (type == BannerType.ClearArt)
                        banner = fanarttvDownloader.DownloadBanner(searchResult.Id, BannerType.ClearArt);
                    if (banner != null)
                    {
                        if (!banner.IsLoaded && banner.Id > 0)
                        {
                            if (banner.LoadBanner())
                            {
                                while (banner.BannerLoading)
                                {
                                }
                                banner.BannerImage.Save(Path.Combine(dirSerie.FullName, type.ToString() + ".jpg"));
                                Console.WriteLine("Saving " + type.ToString() + " for: " + dirSerie.Name);
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(banner.BannerPath))
                            {
                                WebClient client = new WebClient();
                                client.DownloadFile(banner.BannerPath, Path.Combine(dirSerie.FullName, type.ToString() + Path.GetExtension(banner.BannerPath)));
                                Console.WriteLine("Saving " + type.ToString() + " for: " + dirSerie.Name);
                            }
                        }

                    }
                }
            }
        }

    }
}
